/**
 * Created by Administrator on 2017/8/15.
 */

var mysql = require('mysql');
var runType = require('config').get('runType');

switch (runType){
    case 'produce':
        var nodeGjjDB = mysql.createPool({
            host: '',
            user: '',
            password: '',
            database: ''
        });
        break;
    case 'development':
        var nodeGjjDB = mysql.createPool({
            host: '127.0.0.1',
            user: 'root',
            password: 'root',
            database: 'node_gjj'
        });
        break;
    case 'test':
        var nodeGjjDB = mysql.createPool({
            host: '118.24.46.19',
            user: 'root',
            password: 'zhangzhibo',
            database: 'node_gjj'
        });
        break;
    default:
        var nodeGjjDB = mysql.createPool({
            host: '127.0.0.1',
            user: 'root',
            password: 'root',
            database: 'node_gjj'
        });
        break;
}


var DB = {
    queryAsync:function(sql,post){
        return new Promise(resolve => {
            this['myPool'].getConnection(function(error,connection){
                if(!error){
                    connection.query(sql,post,function(error,rows){
                        connection.release();//释放链接
                        if(!error){
                            resolve(rows);
                        }else{

                            throw error;
                        }
                    });
                }else{
                    throw error;
                }
            });
        });
    },
    query:function(sql, post, callback) {
        this['myPool'].getConnection(
            function (err, connection) {
                // Use the connection
                if (err) {
                    console.error(err);
                    return;
                }
                connection.query(sql, post, function (err, rows) {
                    callback(err, rows);
                    connection.release();//释放链接
                });
            }
        );
    },
    getRowAsync:function(sql,post){
        return new Promise(resolve => {
            this['myPool'].getConnection(function(error,connection){
                if(!error){
                    connection.query(sql,post,function(error,rows){
                        connection.release();//释放链接
                        if(!error){
                            var row;
                            if(!rows.length<1){
                                row = rows[0];
                            }else{
                                row = false;
                            }
                            resolve(row);
                        }else{
                            throw error;
                        }
                    });
                }else{
                    throw error;
                }
            });
        });
    },
    getRow: function(sql, post, callback) {
        this['myPool'].getConnection(function (err, connection) {
            // Use the connection
            if (err) {
                console.error(err);
                return;
            }
            connection.query(sql, post, function (err, rows) {
                if(!err){
                    var row;
                    if(!rows.length<1){
                        row = rows[0];
                    }else{
                        row = false;
                    }
                    callback(false, row);
                    connection.release();//释放链接
                }else{
                    callback(err)
                    connection.release();
                }

            });
        });
    },
    getDB:function(databasesName){
        try{
            this.myPool = eval(databasesName);
            return this;
        }catch (error){
            console.log(databasesName+' is undefined');
            return false;
        }
    }
}

module.exports = DB;
